# -*- coding:utf-8 -*-
"""
Created on 1 okt 2011

@author: Olof Manbo
"""

from google.appengine.api import urlfetch

import test_pb2
import logging


#URL = "http://sveanas.appspot.com/simulatorwebservice/api/proto"
#URL = "http://uhrqvist.appspot.com/simulatorwebservice/api/proto"
URL = "http://localhost:8888/simulatorwebservice/api/proto"
URL_PROCESS = URL + "/process"
URL_NODE = URL + "/node"

def sendProcessRequest(type, argument):
    header = {"Content-Type": "application/x-www-form-urlencoded"}
    processRequest = test_pb2.ProcessRequest()
    processRequest.command = type
    processRequest.argument = argument
    params = processRequest.SerializeToString()
    result = urlfetch.fetch(url = URL_PROCESS,
                            payload = params,
                            method = urlfetch.POST,
                            headers = header,
                            deadline = 10)
    r1 = result
    data1 = r1.content
    processResponse = test_pb2.ProcessResponse()
    processResponse.ParseFromString(data1)
    return processResponse

def sendNodeRequest():
        header = {"Content-Type": "application/x-www-form-urlencoded"}
            
        nodeRequest = test_pb2.NodeRequest()
        
        mNodes = nodeRequest.getNodes.add()
        
        mNodes.x = 0
        mNodes.y = 0
        mNodes.width = 100000
        mNodes.height = 100000
        
        params = nodeRequest.SerializeToString() 
            
        result = urlfetch.fetch(url = URL_NODE,
                                payload = params,
                                method = urlfetch.POST,
                                headers = header,
                                deadline = 10)
        nodeResponse = test_pb2.NodeResponse()
        logging.info(str(nodeResponse))
        nodeResponse.ParseFromString(result.content)
        return nodeResponse